package com.org.redis.utils;

import com.org.redis.config.JedisUtil;
import redis.clients.jedis.Jedis;

public class UrlUtil {

    /** redis自增key **/
    static final String SHORT_URL_KEY = "SHORT_URL_KEY";

    /**  网页前缀 **/
    static final String LOCALHOST = "http://jy/";

    /** 记录原网址链接 **/
    static final String SHORT_LONG_PREFIX = "short_long_prefix_";

    /** 记录原网址链接 **/
    static final String CACHE_KEY_PREFIX = "cache_key_prifix_";


    public String getShortUrl(String longUrl){
        Jedis jedis = JedisUtil.getJedis();
        String cache = jedis.get(CACHE_KEY_PREFIX + longUrl);
        if(cache != null){
            return LOCALHOST + CodeUtil.toOtherBaseString(Long.valueOf(cache),64);
        }
        long num = jedis.incr(SHORT_URL_KEY);
        jedis.set(SHORT_LONG_PREFIX + num,longUrl);

        jedis.set(CACHE_KEY_PREFIX + longUrl,String.valueOf(num));
        return LOCALHOST + CodeUtil.toOtherBaseString(num,64);

    }

    public static void main(String[] args) {
        UrlUtil urlUtil = new UrlUtil();
        String url = "http://134.175.117.176/jzwl";
        System.out.println(urlUtil.getShortUrl(url));
    }


}
